package com.chinaoly.baotou.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinaoly.baotou.common.utils.RsaUtil;
import com.chinaoly.baotou.model.MessageParams;
import com.chinaoly.baotou.service.SmsService;
import com.chinaoly.frm.core.entity.Result;
import com.chinaoly.frm.core.entity.ResultGenerator;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SmsServiceImpl implements SmsService {
    @Value("${sms.url}")
    private String url;
    @Value("${sms.userName}")
    private String userName;
    @Value("${sms.secretKey}")
    private String secretKey;
    @Override
    public Result send(MessageParams messageParams) {
        return sendDx(messageParams);
    }

    //短信接口调用（正式）
    private String getToken() {
        String getTokenUrl = url + "/sys/getMsgToken";
        try {
            HttpClient httpclient = new HttpClient();
            PostMethod post = new PostMethod(getTokenUrl);
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
            post.addParameter("userName", userName);
            post.addParameter("secretKey", secretKey);
            post.addParameter("sign", RsaUtil.encrypt(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")));
            httpclient.executeMethod(post);
            String re = new String(post.getResponseBody(), "utf-8");
            JSONObject json = JSONObject.parseObject(re);
            if (json.getBoolean("success")) {
                return json.getString("result");
            } else {
                System.out.println(re);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*sms:
    center:
    url: https://sm.tbkjkf.com:8443/server-api
    userName: gasf_oazf
    secretKey: b2eb193754c5a4666a5927e5477a6121
    rsaPublicKey: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQZ7a/DGt0zuWHdT2JxXZX4cxSOeo8SB0BKkqYzdPw1fxwiP/JkjiMY6CaJyj+hcjvSR6r7AbWVjFc7fwmLDfPzYJ/rH9CQ0J5rnEeTmt2Zbum9jnxH3LVTLuXkARxnXopLQHUo+YOOuoD29ig5C6yb1cCTL5vE+kI1xzVsM1FgwIDAQAB
    sign: OA*/

    //短信接口调用
    private Result sendDx(MessageParams messageParams) {
        String sendUrl = url + "/send/sendMessage";
        try {
            HttpClient httpclient = new HttpClient();
            PostMethod post = new PostMethod(sendUrl);
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
            post.setRequestHeader("Content-type", "application/json; charset=utf-8");
            post.setRequestHeader("X-Access-Token", getToken());
            StringRequestEntity requestEntity = new StringRequestEntity(JSONObject.toJSONString(messageParams),"text/xml","UTF-8");
            post.setRequestEntity(requestEntity);//短信流水号
            httpclient.executeMethod(post);
            String re = new String(post.getResponseBody(), "utf-8");
            System.out.println(re);
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultGenerator.genFailResult("短信发送失败"+e.getMessage());
        }
        return  ResultGenerator.genSuccessResult("短信发送成功");
    }
}
